﻿using System;
using System.Windows.Forms;
using Td.Additional.Environment;
using Td.Additional.Runtime;
using TestApp.Xml;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InstanceManager.ManagerInstance.NewInstanceCreated += ManagerInstance_NewInstanceCreated;
            InstanceManager.ManagerInstance.DataReceived += ManagerInstance_DataReceived;
            label1.Text = "No other instance is running";
            label2.Text = "No data received";
        }

        private void ManagerInstance_DataReceived(object sender, Td.Additional.Runtime.DataReceivedEventArgs e)
        {
            BeginInvoke((MethodInvoker)delegate() { label2.Text += Environment.NewLine + e.ToString(); });
        }

        private void ManagerInstance_NewInstanceCreated(object sender, EventArgs e)
        {
            BeginInvoke((MethodInvoker)delegate()
            {
                if (WindowState == FormWindowState.Minimized)
                    WindowState = FormWindowState.Normal;
                TopMost = true;
                TopMost = false;
                Activate();
                label1.Text = "Another instance is started";
            });
        }

        private void buttonXmlTest_Click(object sender, EventArgs e)
        {
            XmlTestForm xmlTestForm = new XmlTestForm();
            xmlTestForm.ShowDialog();
        }

        private void buttonTestParameterParser_Click(object sender, EventArgs e)
        {
            var args = new String[]
            {
                "/Nags:Baby;Sit",
                "/LeaderName:Manni",
                "/LeaderPassword:Diego",
                "/Help",
            };
            CommandLineParser parser = new CommandLineParser();
            parser.Arguments = args;
            var param = new ParameterClass();
            if (!parser.FillParameterClass(param))
                MessageBox.Show("Fehler: " + parser.Error);
            MessageBox.Show(parser.GetHelp());
            var message = "Nags = ";
            foreach (var item in param.Nags)
                message += item + ", ";
            message += Environment.NewLine;
            message += "Leader = " + param.LeaderName + Environment.NewLine;
            message += "LeaderId = " + param.LeaderId.ToString() + Environment.NewLine;
            message += "LeaderPassword = " + param.LeaderPassword + Environment.NewLine;
            message += "Help = " + param.Help + Environment.NewLine;
            MessageBox.Show(message);
        }
    }
}